home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: twisto.eng.hou.compaq.com!news
- From: Saurabh Dixit <saurabhd@bangate.compaq.com>
- Subject: IsA v/s HasA problem
- Message-ID: <30F52C66.851@bangate.compaq.com>
- Sender: news@twisto.eng.hou.compaq.com (System Administrator)
- Mime-Version: 1.0
- X-Mailer: Mozilla 2.0b2 (Windows; I; 32bit)
- Content-Type: text/plain; charset=us-ascii
- Organization: Compaq Computer Corporation
- Date: Thu, 11 Jan 1996 15:27:34 GMT
- X-Nntp-Posting-Host: 172.18.176.8
- Content-Transfer-Encoding: 7bit
-
- Hi!
-
- I always had no trouble distinguishing between the two...
-
- But now, I for a specific case, I fail to see the repercussions of
- using the wrong relationship between objects.
-
- This is my scenario.
- I have an object of type Base. This is an abstract class.
- From Base I derive several different objects.
- I want to maintain a list of such objects for which I have a class
- Collection that encapsulates a typed array of Base objects.
-
- I now want to define an object - let's call it Duh - that maintains a
- collection of Base objects. Duh objects are entities on their own and could
- have other attributes, BUT NEVER ANOTHER COLLECTION.
-
- From what I gather, I am then leaning towards a HasA relationship, i.e.
- class Collection
- {
- public:
- BOOL Add (Base* b);
- };
- class Duh
- {
- private:
- Collection c;
- };
-
- Obviously if I was going to have 2 or more collections, then I would have had
- to have a HasA relationship. But as said earlier, this is not the case!
- Also, I want to have some operations common between Collection and Duh.
- e.g. I can Add() a Base (derived!) object to a Collection and I also want
- to Add() an object to Duh. In a HasA relationship I would simply
- implemenent this as follows.
- BOOL Duh::Add (Base* b)
- {
- // simply pass on to Collection method
- return c.Add (b);
- }
- However for an IsA relationship, I would simply do
- class Duh : public Collection
- {
- };
- and I am done.
-
- Am I wasting my time too much thinking about this? Regardless of code
- implementation, from the logical perspective, what is the correct object-
- oriented solution for my problem, HasA or IsA?
-
- Any help will be greatly appreciated
- thanks
- saurabh
- --
- Saurabh Dixit
- "All opinions are always my own..."
- Compaq Computer Corporation
- ------------------------------------------------------------------------
- Thou who sneezes without kerchief takes matters in own hands.
- - SomeoneIOnceNew
- ------------------------------------------------------------------------
- xxxx
-